#include "Engine.h"
#include "Core/Core.h"
#include "Process/Process.h"
#include "XML\XMLTreeNode.h"
#include "RenderManager.h"
#include "InputManager.h"
#include "Logger\Logger.h"


CEngine::CEngine()
: m_pCore(NULL)
, m_pProcess(NULL)
, m_timer(30)
, m_pLogger(NULL)
{}


CEngine::~CEngine()
{
	DeInit();
}

void CEngine::Init(HWND hWnd)
{
	m_pLogger = new CLogger();
	m_pCore = new CCore();
	m_pCore->Init(hWnd, m_sEngineInitParams);
	m_pProcess->Init();
	
}

void CEngine::DeInit()
{
	CHECKED_DELETE(m_pCore);
	CHECKED_DELETE(m_pProcess);
	CHECKED_DELETE(m_pLogger);
}

void CEngine::Update()
{
	m_timer.Update();
	UpdateActions();
	m_pCore->Update(m_timer.GetElapsedTime());
	m_pProcess->Update(m_timer.GetElapsedTime());
}

void CEngine::UpdateActions()
{
	if(CORE->GetInputManager()->IsUpDown(IDV_KEYBOARD, KEY_X))
	{
		m_pProcess->SwapDebugInfo();
	}
}

void CEngine::Render()
{
	CRenderManager* RM = m_pCore->GetRenderManager();
	RM->BeginRendering(); //Setear los estados de pintado

	CCamera* l_pCamera = m_pProcess->GetCamera();
	RM->SetupMatrices(l_pCamera);
	{
		RenderScene(RM);
	}
	RM->EndRendering();


}

void CEngine::RenderScene(CRenderManager* RM)
{
	//Pintado 3d:
	m_pCore->Render();
	m_pProcess->RenderScene(RM);

	//Printado 2d:
	RM->DisableZBuffering();
	RM->EnableAlphaBlend();

	
	m_pProcess->RenderScene2D(RM, m_pCore->GetFontManager());

	uint32 l_height = m_pProcess->RenderDebugInfo(RM, m_pCore->GetFontManager(), m_timer.GetFPS());
	if(m_pProcess->GetDebugInfo())
	{
		RM->DrawRectangle2D(Vect2i(0, 0), 385, l_height, colGREY, 3, 3, colBLACK);
	}
	else
	{
		RM->DrawRectangle2D(Vect2i(0, 0), 340, l_height, colGREY, 3, 3, colBLACK);
	}
	m_pProcess->RenderDebugInfo(RM, m_pCore->GetFontManager(), m_timer.GetFPS());
	//m_pCore->GetGUIManager()->Render();

	RM->EnableZBuffering();
	RM->DisableAlphaBlend();

}

void CEngine::SetProcess(CProcess* p)
{
	m_pProcess = p;
}

void CEngine::LoadXML(const std::string& pathInitXML)
{
	CXMLTreeNode l_XMLParser;
	if (!l_XMLParser.LoadFile(pathInitXML.c_str()))
	{
		printf("ERROR loading the file.\n");
		//TODO_OSCAR notificar l'error d'alguna manera (return de la funcio?)
	}
	
	CXMLTreeNode  m = l_XMLParser["Config"];
	if (m.Exists())
	{
		int count = m.GetNumChildren();
		for (int i = 0; i < count; ++i)
		{

			std::string aa = m(i).GetName();

			if(!strcmp(m(i).GetName(),"ScreenResolution"))
			{
				m_sEngineInitParams.m_ScreenResolution = m(i).GetVect2iProperty("resolution", (600, 600));
			}
			else if(!strcmp(m(i).GetName(),"WindowsPosition"))
			{
				m_sEngineInitParams.m_WindowsPosition = m(i).GetVect2iProperty("position", (100, 100));
			}
			else if(!strcmp(m(i).GetName(),"FullScreen"))
			{
				m_sEngineInitParams.m_bFullScreen = m(i).GetBoolProperty("fullScreenMode", false);
			}
			else if(!strcmp(m(i).GetName(),"Languages"))
			{
				m_sEngineInitParams.m_DefaultLanguage = m(i).GetPszProperty("default");
				CXMLTreeNode n = l_XMLParser["Languages"];
				if (n.Exists())
				{
					int count = n.GetNumChildren();
					for (int j = 0; j < count; ++j)
					{
						m_sEngineInitParams.m_LaguagesXML.push_back(n(j).GetPszProperty("languageXML"));
					}
				}
			}
			else if(!strcmp(m(i).GetName(),"Fonts"))
			{
				m_sEngineInitParams.m_FontsXML = m(i).GetPszProperty("fontsXML");
			}
			else if(!strcmp(m(i).GetName(),"MouseMode"))
			{
				m_sEngineInitParams.m_bExclusiveModeinMouse = m(i).GetBoolProperty("exclusiveMode");
			}
		}
	}
}